home *** CD-ROM | disk | FTP | other *** search
/ Aminet 15 / Aminet 15 - Nov 1996.iso / Aminet / dev / lang / FPL_v147.lha / fpl / funclib / test.c < prev    next >
C/C++ Source or Header  |  1994-05-09  |  5KB  |  169 lines

  1. /************************************************************************
  2.  *                                                                      *
  3.  * fpl.library - A shared library interpreting script langauge.         *
  4.  * Copyright (C) 1992-1994 FrexxWare                                    *
  5.  * Author: Daniel Stenberg                                              *
  6.  *                                                                      *
  7.  * This program is free software; you may redistribute for non          *
  8.  * commercial purposes only. Commercial programs must have a written    *
  9.  * permission from the author to use FPL. FPL is *NOT* public domain!   *
  10.  * Any provided source code is only for reference and for assurance     *
  11.  * that users should be able to compile FPL on any operating system     *
  12.  * he/she wants to use it in!                                           *
  13.  *                                                                      *
  14.  * You may not change, resource, patch files or in any way reverse      *
  15.  * engineer anything in the FPL package.                                *
  16.  *                                                                      *
  17.  * This program is distributed in the hope that it will be useful,      *
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                 *
  20.  *                                                                      *
  21.  * Daniel Stenberg                                                      *
  22.  * Ankdammsgatan 36, 4tr                                                *
  23.  * S-171 43 Solna                                                       *
  24.  * Sweden                                                               *
  25.  *                                                                      *
  26.  * FidoNet 2:201/328    email:dast@sth.frontec.se                       *
  27.  *                                                                      *
  28.  ************************************************************************/
  29.  
  30. #ifdef AMIGA
  31. #include <exec/types.h>
  32. #include <proto/exec.h>
  33.  
  34. int CXBRK(void) { return(0); }  /* Disable Lattice/SAS CTRL/C handling */
  35. int chkabort(void) { return(0); }  /* really */
  36.  
  37. #include <exec/libraries.h>
  38. #include <libraries/dos.h>
  39.  
  40. #include <pragmas/FPL_pragmas.h>
  41. #include <clib/FPL_protos.h>
  42. #include <libraries/FPL.h>
  43. struct Library *FPLBase = NULL;
  44.  
  45. #define REG(x) register __ ## x
  46.  
  47. #elif defined(UNIX) /* #ifdef AMIGA */
  48.  
  49. #error can't be compiled!
  50.  
  51. #endif
  52.  
  53. #include <stdlib.h>
  54. #include <string.h>
  55. #include <stdio.h>
  56. #include <stdarg.h>
  57.  
  58. #define CALLER __saveds
  59. #define ASM __asm
  60.  
  61. long ASM func(REG(a0) struct fplArgument *);
  62.  
  63.  
  64. enum myfunctions {
  65.   FN_OUTPUT,
  66.   };
  67.  
  68. /**********************************************************************
  69.  *
  70.  * int main(int, char **)
  71.  *
  72.  * This function is not included in the run time library version.
  73.  *
  74.  ******/
  75.  
  76. void *key;
  77.  
  78. int main(int argc, char **argv)
  79. {
  80.   long end=0;
  81.  
  82. #if defined(AMIGA)
  83.   if(!(FPLBase=OpenLibrary(FPLNAME, 7))) {
  84.     printf("Error opening %s!\n", FPLNAME);
  85.     return(-1);
  86.   }
  87.   printf("--> %s\n", FPLBase->lib_IdString);
  88. #endif
  89.  
  90.   if(argc<2) {
  91.     printf("Usage: test <FPL program file name>\n");
  92. #if defined(AMIGA)
  93.     CloseLibrary((struct Library *)FPLBase);
  94. #endif
  95.     return 0;
  96.   }
  97.  
  98.   key=fplInitTags(func,
  99.                   FPLTAG_STACK, 20000,
  100.                   FPLTAG_MINSTACK, 4000,
  101.                   FPLTAG_END);
  102.  
  103.   fplAddFunction(key, "output",       FN_OUTPUT,    'I', "O", NULL);
  104.  
  105.   end=fplExecuteFile(key, argv[1], NULL);
  106.   
  107.   fplFree(key); /* free all */
  108.  
  109. #if defined(AMIGA)
  110.   CloseLibrary((struct Library *)FPLBase);
  111. #endif
  112.   return end;
  113. }
  114.  
  115. long ASM func(REG(a0) struct fplArgument *arg)
  116. {
  117.   int ret;
  118.   long col;
  119.   char *name;
  120.   char *string;
  121.   void *anchor=arg->key;
  122.   switch(arg->ID) {
  123.   case FN_OUTPUT: /* output */
  124.     if(arg->format[0]==FPL_STRARG)  /* we got a string! */
  125.       string="%s";
  126.     else
  127.       string="%d";
  128. #if defined(AMIGA)
  129.     printf(string, arg->argv[0]);
  130. #elif defined(UNIX)
  131.     fprintf(stderr, string, arg->argv[0]);
  132. #endif
  133.     fplSendTags(anchor, FPLSEND_INT, 1, FPLSEND_DONE);
  134. #if 0
  135.     if(count++>10)
  136.       return(FPLERR_PROGRAM_STOPPED);
  137. #endif
  138.     break;
  139.  
  140.   case FPL_GENERAL_ERROR:
  141.     {
  142.       char buffer[FPL_ERRORMSG_LENGTH];
  143.       fplSendTags(anchor,
  144.           FPLSEND_GETVIRLINE, &col,
  145.           FPLSEND_GETVIRFILE, &name,
  146.           FPLSEND_DONE);
  147.       if(*name=='\"') {
  148.     ret=0;
  149.     name++;
  150.     while(name[ret] && name[ret]!='\"')
  151.       ret++;
  152.     string=(char *)fplAlloca(anchor, ret+1);
  153.     memcpy(string, name, ret);
  154.     string[ret]='\0';
  155.       } else {
  156.     string=name;
  157.     ret=0;
  158.       }
  159.       printf("\n>>> %s\n",
  160.          fplGetErrorMsg(arg->key, (long)arg->argv[0], buffer));
  161.       printf(">>> Line %d in file \"%s\". <<<\n", col, string);
  162.       if(ret)
  163.     fplDealloca(anchor, string);
  164.     }
  165.     break;
  166.   }
  167.   return(0);
  168. }
  169.